<?php
!defined('IN_AM') && exit('Access Denied');
class control extends base
{
	function control()
	{
		$this->base();
		$this->load_class('photo');
		$this->load_class('album');
		$this->load_class('user');
		$this->load_class('comment');
	}
	function actionupload()
	{
		require_once AM_ROOT.'levelconfig.inc.php';
		if(isset($_POST['submitted']))
		{
			include(AM_ROOT.'lib/function_image.php');
			$allowfiletype = array('jpg','gif','png');
			$uid = $_SESSION['user_id'];
			$csize = 0;
			$cfile = 0;//单次上传文件总数
			$userdata = $_ENV['user']->userdata();//用户数据
			$totalsize = $userdata[0]['totalsize'];//已用空间
			$level = $_SESSION['level'];
			$freesize = $levelconfig[$level]['allow_total_size']-$totalsize;//剩余空间
			$_POST = escape($_POST);
			if($_POST['albumop'] == 'selectalbum')
			{
				$albumid = intval($_POST['albumid']);
			}
			elseif($_POST['albumop'] == 'creatalbum')
			{
				$albumname = $_POST['albumname']?substr($_POST['albumname'],0,16):date('Ymd');
				$ctime = time();
				$row = array(
					'user_id'=>"$uid",
					'title'=>"$albumname",
					'description'=>"",
					'created'=>"$ctime",
					'updatetime'=>"$ctime",
					'url'=>'',		
				);
				$albumid = $_ENV['album']->create($row);
			}
			$result = $_ENV['album']->checkpermission($albumid,$uid);
			if(!$result)
				{
					$this->redirect('index.php?m=album&a=create',4000,'创建相册失败请重试');
					exit();
				}
			$max_upload = 3;//单次允许上传文件数量
			$max_file_size = 1048576;//允许单文件最大为1M
			$upcount = count($_FILES['photo']['name']);//图片数量
			//var_dump($_FILES['photo']['name']);
			//exit();
			if($upcount>$max_upload)
			{
				$this->redirect('index.php?m=photo&a=upload',4000,'单次上传图片不能超过三张');
				exit();
			}
			if(empty($_FILES['photo']['name'][0]))//解决上传文件少于三个时数组第一个为空的问题
			$key=1;
			else
			$key=0;
			for(;$key<$upcount;$key++)
			{
				$title = $_FILES['photo']['name'][$key];
				$ctime = time();
				$fileext = strtolower(trim(substr(strchr($title,'.'),1)));
				if(!in_array($fileext,$allowfiletype))
				{
					$this->redirect('index.php?m=photo&a=upload',4000,'不支持该图片类型');
					exit();
				}
				$filesize = $_FILES['photo']['size'][$key];
				if($filesize > $max_file_size)
				{
					$this->redirect('index.php?m=photo&a=upload',4000,'上传图片过大');
					exit();
				}				
				$ctime = time();
				$photoname = $_SESSION['user_id'].'_'.$ctime.mt_rand(100,999).'.'.$fileext;
				$filetype = $_FILES['photo']['type'][$key];
				$path = date('Ym').'/'.date('d');
				//file_exists(AM_UPLOAD."$path")?'':mkdir(AM_UPLOAD."$path");
				//file_exists(AM_UPLOAD."$path".'/'."$path2")?'':mkdir(AM_UPLOAD."$path".'/'."$path2");
				mkdirs(AM_UPLOAD.$path);
				$url = $path.'/'."$photoname";
				$up_result = move_uploaded_file($_FILES['photo']['tmp_name'][$key],AM_UPLOAD.$url);
				$thumbpath = makethumb(AM_UPLOAD.$path.'/'."$photoname");
				$thumb = empty($thumbpath)?'0':1;	
				if($up_result)
				{
					$csize += $filesize;
					$cfile++;
					$row = array(
					'album_id'=>"$albumid",
					'user_id'=>"$uid",
					'title'=>"$title",
					'created'=>"$ctime",
					'filename'=>"$photoname",
					'filesize'=>"$filesize",
					'type'=>"$filetype",
					'url'=>"$url",
					'thumb'=>"$thumb",					
					);
					
					$freesize -=$filesize;
					if($freesize<0)
					{
						$this->redirect('index.php?m=photo&a=upload',2000,'剩余空间不足');
						exit();
					}
					$result = $_ENV['photo']->upload($row);
					if(!$result)
					{
						$this->redirect('index.php?m=photo&a=upload',4000,'入库失败,请重试');
						exit();
					}
				}
				else
				{
					$this->redirect('index.php?m=photo&a=upload',4000,'上传失败,请重试!');
					exit();
				}
			}
			$row2 = array(
			'updatetime'=>"$ctime",
			'photos_count'=>'photos_count+'."$cfile",
			'thumburl'=>"'"."$url"."'",
			);
			$result2= $_ENV['album']->update($row2,$albumid);
			$row3 = array(
			'totalsize'=>'totalsize+'."$csize",
			);
			$result3 = $_ENV['user']->update($row3);
			$this->redirect('index.php?m=album&a=view&albumid='.$albumid,4000,'上传成功');
			exit();
		}
		$uid = $_SESSION['user_id'];
		$userdata = $_ENV['user']->userdata();//用户数据
		$usedsize = $userdata[0]['totalsize'];//已用空间
		$level = $_SESSION['level'];		
		$percentage = $usedsize/$levelconfig[$level]['allow_total_size'];
		$getalbumid = isset($_GET['albumid'])?intval($_GET['albumid']):'';
		$this->view->assign('getalbumid',$getalbumid);
		$this->view->assign('usedsize',round($usedsize/(1024*1024),2));
		$this->view->assign('max_size',$levelconfig[$level]['allow_total_size']/(1024*1024));
		$this->view->assign('percentage',round($percentage,2)*100);
		$albumlist = $_ENV['album']->albumlist($uid);
		$this->view->assign('albumlist',$albumlist);
		$this->view->display('upload.htm');
	}
	function actionphoto_view()
	{
		$photoid = isset($_GET['photoid'])?intval($_GET['photoid']):0;
		$cpage = isset($_GET['cpage'])?intval($_GET['cpage']):1;
		$albumdata = $_ENV['photo']->get_albumid($photoid);
		if(!empty($albumdata))
		{
			$cperpage  = 5;//每页评论数
			$cf = ($cpage-1)*$cperpage;//查询开始数
			$albumid = $albumdata['album_id'];
			if(empty($_GET['goto'])) $_GET['goto'] = '';
			if($_GET['goto'] =='up')
			{
				$photodata = $_ENV['photo']->get_last($photoid,$albumid);
				$photoid = $photodata['photo_id'];
			}
			elseif($_GET['goto']=='down')
			{
				$photodata = $_ENV['photo']->get_next($photoid,$albumid);				
				$photoid = $photodata['photo_id'];
			}
			else
			{
				$photoid = intval($_GET['photoid']);
				$photodata = $_ENV['photo']->photoview($photoid);				
			}
			$uid = $photodata['user_id'];
			if($uid!=getgpc('user_id','S'))
			{
				$row=array('pviewnum'=>'pviewnum+1');
				$_ENV['photo']->update($row,$photoid);
			}
			$cnum = $_ENV['comment']->get_count_by_id($photoid,1);//获得评论记录总数
			$commentdata = $_ENV['comment']->comment_list($photoid,1,$cf,$cperpage);
			$commentmulti = $this->multi($cnum,$cperpage,$cpage,'index.php?m=album&a=view&albumid='.$albumid,'cpage');
			$this->view->assign('commentmulti',$commentmulti);
			$this->view->assign('commentdata',$commentdata);
			$this->view->assign('photodata',$photodata);
			$this->view->display('photo_view.htm');
		}
		else
		{
			$this->redirect('index.php',0,'没找到需要的图片');
		}
	}
	function actionedit()
	{

		if(!empty($_POST['title']))
		{
			$_POST = escape($_POST);
			$photoid = intval(getgpc('photoid','P'));
			$photodata = $_ENV['photo']->photoview($photoid);
			empty($photodata) && exit('无效相片ID');
			$uid = $photodata['user_id'];
			if($uid != getgpc('user_id','S'))
			{
				$this->redirect('index.php',4000,'无权限访问该页面');
				exit();
			}
			$title = $_POST['title'];
			$pdescription = !empty($_POST['pdescription'])?$_POST['pdescription']:'';
			$row  = array(
			'title'=>'\''."$title".'\'',
			'pdescription'=>'\''."$pdescription".'\'',
			);
			$result = $_ENV['photo']->update($row,$photoid);
			if($result)
			{
				$this->redirect('index.php?m=photo&a=photo_view&photoid='.$photoid,4000,'更新成功');
				exit();
			}				
		}
		$photoid = intval(getgpc('photoid','G'));
		$photodata = $_ENV['photo']->photoview($photoid);
		empty($photodata) && exit('无效相片ID');
		$this->view->assign('photodata',$photodata);
		$this->view->display('photo_edit.htm');
	}
	function actiondelete()
	{
		$photoid = intval(getgpc('photoid'),'G');
		$uid = getgpc('user_id','S');
		empty($uid) && exit('无权限');
		$photodata = $_ENV['photo']->photoview($photoid);
		$result = $_ENV['photo']->delete($photoid,$uid);
		if($result)
		{
			$row = array('photos_count'=>'photos_count-1');
			$albumid = $photodata['album_id'];
			$_ENV['album']->update($row,$albumid);
			$url = $photodata['url'];
			@unlink(AM_UPLOAD.$url);
			@unlink(AM_UPLOAD.$url.'.thumb.jpg');
			$this->redirect('index.php?m=album&a=albumlist#',4000,'删除成功');
			exit();
		}
		else
		{
			$this->redirect('index.php',4000,'没有权限');
			exit();
		}
	}
	function actionhot()
	{
		$page = is_numeric(getgpc('page','G'))?intval(getgpc('page','G')):'1';
		$perpage =9;
		$from = ($page-1)*$perpage;
		$num = $_ENV['photo']->get_count();
		$photodata = $_ENV['photo']->get_hot($from,$perpage);
		$multi = $this->multi($num,$perpage,$page,'index.php?m=photo&a=hot','page');
		$this->view->assign('multi',$multi);
		$this->view->assign('photodata',$photodata);
		$this->view->display('photo_hot.htm');
	}	
}
?>